package com.lean.android.adsdk

import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.fragment.app.FragmentActivity
import com.lean.android.ad.core.AdLoaders
import com.lean.android.ad.core.AdSdk
import com.lean.android.ad.core.PrivacyDialogBlockChain
import com.lean.android.ad.core.VipDialogBlockChain
import com.lean.android.ad.utils.StatusBarHelper
import com.lean.android.ad.workflow.WorkFlows
import com.lean.android.adsdk.databinding.ActivityMainBinding


class MainActivity : FragmentActivity(), WorkFlows.MainFlowCallback {

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        StatusBarHelper.translucent(this)
        StatusBarHelper.setStatusBarLightMode(this)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        WorkFlows.startMainFlow(this, this)

        binding.btnInsert.onThrottleClick {
            AdLoaders.startInterstitialAd(this)
        }

        binding.btnLocalInsert.onThrottleClick {
            AdLoaders.startInterstitialAd(this)
        }

        binding.btnReward.onThrottleClick {
            AdLoaders.startRewardAd(this, onReward = {
                Toast.makeText(this, "成功获得奖励", Toast.LENGTH_SHORT).show()
            }, onInvalid = {
                Toast.makeText(this, "获得奖励失败", Toast.LENGTH_SHORT).show()
            })
        }

        binding.btnLocalReward.onThrottleClick {

        }

        binding.btnFeedAd.onThrottleClick {
            val intent = Intent(this, FeedAdActivity::class.java)
            startActivity(intent)
        }

        binding.btnSplashAd.onThrottleClick {
            val intent = Intent(this, LauncherActivity::class.java)
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
            startActivity(intent)
        }

        binding.btnTestTools.isChecked = AdSdk.isVip
        binding.btnTestTools.setOnCheckedChangeListener { buttonView, isChecked ->
            Toast.makeText(this, "广告开关： $isChecked", Toast.LENGTH_SHORT).show()
            AdSdk.isVip = isChecked

            if (isChecked) {
                AdLoaders.removeCurrentFeedAd(this)
            }
        }

        AdLoaders.startFeedAd(this, binding.feedContainer)
    }

    override fun showPrivacyDialog(chain: PrivacyDialogBlockChain) {
        PrivacyDialog(this, onAgree = {
            chain.onAccept {}
        }, onDisagree = {
            chain.onDenied()
        }).show()
    }

    override fun showVipDialog(chain: VipDialogBlockChain) {
        DialogHelper.showVipDialog(this, R.layout.dialog_vip_cbrowser, onConfirm = {
            chain.onAccept({
                it()
            }, true)
        }, onCancel = {
            chain.onDenied()
        })
    }

}